<?php

/**
 * Simple excel generating from PHP5
 *
 * @package Utilities
 * @license http://www.opensource.org/licenses/mit-license.php
 * @author Oliver Schwarz <oliver.schwarz@gmail.com>
 * @version 1.0
 */

/**
 * Generating excel documents on-the-fly from PHP5
 * 
 * Uses the excel XML-specification to generate a native
 * XML document, readable/processable by excel.
 * 
 * @package Utilities
 * @subpackage Excel
 * @author Oliver Schwarz <oliver.schwarz@vaicon.de>
 * @version 1.1
 * 
 * @todo Issue #4: Internet Explorer 7 does not work well with the given header
 * @todo Add option to give out first line as header (bold text)
 * @todo Add option to give out last line as footer (bold text)
 * @todo Add option to write to file
 */
class ExportExcelLib {
	public function addArray($info) {
		$i = 0;
		$newInfo = array ();
		foreach ( $info as $key => $value ) {
			$j = 0;
			foreach ( $value as $key2 => $value2 ) {
				$newInfo [$i] [$j] = $value2;
				$j ++;
			}
			$i ++;
		}
		return $newInfo;
	}
	
	public function generateXML($name,$info) {
		error_reporting ( E_ALL );
		date_default_timezone_set ( 'Europe/London' );
		
		/** PHPExcel */
		require_once SOURCE_EXT_PATH . 'library/PHPExcel.php';
		
		// Create new PHPExcel object
		$objPHPExcel = new PHPExcel ();
		
		// Set properties
		$objProps = $objPHPExcel->getProperties ();
		$objProps->setCreator ( "Maarten Balliauw" );
		$objProps->setLastModifiedBy ( "Maarten Balliauw" );
		$objProps->setTitle ( "Office 2007 XLSX Test Document" );
		$objProps->setSubject ( "Office 2007 XLSX Test Document" );
		$objProps->setDescription ( "Test document for Office 2007 XLSX, generated using PHP classes." );
		$objProps->setKeywords ( "office 2007 openxml php" );
		$objProps->setCategory ( "Test result file" );
		$zi = array ('A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L', 'M', 'N', 'O', 'P', 'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X', 'Y', 'Z' );
		
		// Add some data
		foreach ( $info as $key => $value ) {
			foreach ( $value as $key2 => $value2 ) {
				$objPHPExcel->setActiveSheetIndex ( 0 )->setCellValue ( $zi [$key2] . ($key+1), $value2 );
			}
		}
		
		// Rename sheet
		$objPHPExcel->getActiveSheet ()->setTitle ( 'Simple' );
		// Set active sheet index to the first sheet, so Excel opens this as the first sheet
		$objPHPExcel->setActiveSheetIndex ( 0 );
		// Redirect output to a client’s web browser (Excel2007)
		header ( 'Content-Type: application/vnd.openxmlformats-officedocument.spreadsheetml.sheet' );
		header ( 'Content-Disposition: attachment;filename="'.$name.'.xlsx"' );
		header ( 'Cache-Control: max-age=0' );
		$objWriter = PHPExcel_IOFactory::createWriter ( $objPHPExcel, 'Excel2007' );
		$objWriter->save ( 'php://output' );
		exit ();
	}

}

?>